package motion;
import java.lang.reflect.Array;

/**
 * @author Matt
 *
 */
public class Vector2D {

	public double force; //Newtons
	public double direction; //Radians
	
	/**
	 * Creates a vector based off of a force and direction
	 * @param force		Can only be positive
	 * @param direction	In radians
	 */
	public Vector2D(double force, double direction) {
		this.force = Math.abs(force);
		this.direction = direction;
	}
	
	/**
	 * Creates a vector based off off of a point that has x and y coordinates
	 */
	public Vector2D(double point[]) {
		double x = point[0];
		double y = point[1];
		this.force = Math.sqrt(Math.pow(x, 2)+Math.pow(y, 2));
		this.direction = Math.atan(y/x);
	}
	
	/**
	 * Adds vectors together to create a new one
	 * @param vectors	List of vectors
	 */
	public Vector2D(Vector2D vectors[]) {
		double totalX = 0;
		double totalY = 0;
		
		int len = Array.getLength(vectors);
		
		for(int i=0; i<len; i++) {
			totalX += this.getXForce(vectors[i]);
			totalY += this.getYForce(vectors[i]);
		}
		
		this.force = this.dist(0, 0, totalX, totalY);
		this.direction = Math.atan2(totalY, totalX);
	}
	
	/**
	 * Gets the distance between two points
	 */
	private double dist(double x1, double y1, double x2, double y2) {
		return Math.sqrt(Math.pow((x1-x2), 2)+Math.pow((y1-y2), 2));
	}
	
	private double getXForce(Vector2D vector) {
		return vector.force*Math.cos(vector.direction);
	}
	private double getYForce(Vector2D vector) {
		return vector.force*Math.sin(vector.direction);
	}

}
